<?php

class FilterBlockHelper extends AppHelper
{
	public $helpers = array('Html', 'Form');
	public $formName;
    public $onclick_callback = '';
    public $action_url = null;
    public $without_form = false;
	protected $rows;
	protected $settings;

	function create($name, $params=array())
	{
	    $this->formName = $name;
	    $this->rows = array(array());
	    $this->settings = array();
	    $result = '';

	    $other_params = array();
		foreach ($params as $key => $value) {
			if (strpos($key, 'filter_')===0) {
				$this->settings[$key] = $value;
			} else {
				$other_params[$key] = $value;
			}
		}

		if (isset($other_params['onclick_callback'])) {
		    $this->onclick_callback = $other_params['onclick_callback'];
		}
		if (isset($other_params['url'])) {
		    $this->action_url = $other_params['url'];
		}
		if (!empty($other_params['without_form'])) {
		    $this->without_form = true;
		    unset($other_params['without_form']);
		}

        $result .= $this->_header();
        $form_begin = $this->Form->create($name, $other_params);
        if (! $this->without_form) {
	       $result .= $form_begin;
        }

	    return $result;
	}

	function add($content)
	{
	    $this->rows[count($this->rows) - 1][] = $content;
	}

	function sep()
	{
		$this->rows[] = array();
	}

	function end()
	{
	    $result = '';

    	$submit1 = '';
    	$submit2 = '';

    	$add_class = 'filter_block_control';
    	$onclick = '';
        if ($this->onclick_callback) {
            $onclick .= 'return '.$this->onclick_callback.'("'.addslashes($this->action_url).'", this);';
        }

    	if (!empty($this->settings['filter_custom_actions']) && isset($this->settings['filter_apply_button'])) {
    		$submit1 = $this->settings['filter_apply_button'];
    	} else {
    	    $attr = array();
    	    $attr["name"] = 'data['.$this->formName.'][submit_apply]';
    	    $attr["class"] = $add_class;
    	    if ($onclick) {
                $attr['onclick'] = $onclick;
    	    }
	        $submit1 = $this->Form->submit(__d(null,'apply_filter', true), $attr);
	    	$submit1 = substr($submit1, strlen('<div class="submit">'));
	    	$submit1 = substr($submit1, 0, strlen($submit1) - strlen('</div>'));
    	}
    	$clear_active = isset($this->data[$this->formName]['submit_apply']) && ($this->data[$this->formName]['submit_apply'] == __d(null,'apply_filter', true));
    	if (!empty($this->settings['filter_custom_actions']) && isset($this->settings['filter_reset_button'])) {
    		$submit2 = $this->settings['filter_reset_button'];
    	} else {
    	    $attr = array();
    	    $attr["name"] = 'data['.$this->formName.'][submit_reset]';
    	    $attr["class"] = $add_class;
    	    if (!$clear_active) {
                $attr['disabled'] = true;
    	    }
    	    if ($onclick) {
                $attr['onclick'] = $onclick;
    	    }
            $submit2 = $this->Form->submit(__d(null,'reset_filter', true), $attr);
        	$submit2 = substr($submit2, strlen('<div class="submit">'));
        	$submit2 = substr($submit2, 0, strlen($submit2) - strlen('</div>'));
    	}
		$this->rows[0][] = $submit1.(strlen($submit1) ? ' ' : '').$submit2;

    	foreach ($this->rows as $count => $row) {
	    	ob_start();
	        ?><table class="filter-container sys_clear<?=($count == (count($this->rows)-1) ? '' : ' b_m05');?>" cellpadding="0"><?php
	        ?><tr class="sys_clear"><?php
        	foreach ($row as $r) {
	            ?><td class="sys_clear nw"><?=$r;?></td><?php
        	}
	        ?></tr><?php
	        ?></table><?php
	        $result .= ob_get_clean();
    	}

        $form_end = $this->Form->end();
        if (! $this->without_form) {
	       $result .= $form_end;
        }
        $result .= $this->_footer();

	    $this->formName = null;
	    $this->rows = array(array());
	    $this->settings = array();

	    return $result;
	}

	protected function _header()
	{
	    ob_start();

	    /*if (empty($this->settings['filter_style']) || $this->settings['filter_style'] != 'inner') {
	        ?><div class="filter-shall"><?php
	    }*/
        /*?><table cellpadding="0" class="filter-container sys_clear"><tr class="sys_clear"><td class="sys_clear"><?php
*/
        return ob_get_clean();
	}

	protected function _footer()
	{
	    ob_start();

  /*      ?></td></tr></table><?php*/
	    /*if (empty($this->settings['filter_style']) || $this->settings['filter_style'] != 'inner') {
        	?></div><?php
	    }*/

        return ob_get_clean();
	}
}

?>
